package com.cjb.cloudsecurity.JWT;

import cn.hutool.core.collection.ListUtil;
import com.cjb.cloudsecurity.userMsg.entity.SysUserEntity;
import com.cjb.cloudsecurity.userMsg.entity.UserDetail;
import com.cjb.common.common.cache.RedisCache;
import com.cjb.common.common.cache.RedisKeys;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

/**
 * @Author chenjinbin
 * @Package IntelliJ IDEA
 * @Class TokenStoreCache
 * @Date 2023-08-20 23:42
 */
@Component
@AllArgsConstructor
public class TokenStoreCache {
    @Autowired
    private  RedisCache redisCache;

    public void saveUser(String accessToken,UserDetail user) {
        String key = RedisKeys.getAccessTokenKey(accessToken);
        redisCache.set(key, user);
    }

    public UserDetail getUser(String accessToken) {
        String key = RedisKeys.getAccessTokenKey(accessToken);
        return (UserDetail) redisCache.get(key);
    }

    public void deleteUser(String accessToken) {
        String key = RedisKeys.getAccessTokenKey(accessToken);
        redisCache.delete(key);
    }

    public List<String> getUserKeyList() {
        String pattern = RedisKeys.getAccessTokenKey("*");
        Set<String> sets = redisCache.keys(pattern);

        return ListUtil.toList(sets);
    }
}

